package com.borya.service.server.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.borya.framework.annotation.Component;
import com.borya.service.server.SendMailService;
import com.borya.statuscode.StatusCode;
import com.borya.util.AsyncUtils;
import com.borya.util.Constant;
import com.borya.util.StringUtil;
import com.borya.util.mail.Smtp;
@Component("sendMailServiceImpl")
public class SendMailServiceImpl implements SendMailService{
	
	
	private Logger log = Logger.getLogger(getClass());
//	public final ExecutorService cachedThreadPool = Executors.newSingleThreadExecutor();
	
	@Override
	public String sendMail(JSONObject jso){
	    if(StringUtil.isNULL(jso.getString("body"))){
	    	return StatusCode.PARAM_ERROR.toJSON();
	    }
	    if(StringUtil.isNULL(jso.getString("content"))){
	    	return StatusCode.PARAM_ERROR.toJSON();
	    }
	    AsyncUtils.executor().execute(()-> {
	    	final String receiver = Constant.receiver;
	    	sendMailing(jso.getString("body"), receiver, jso.getString("content"));
	    });
	    
	    return StatusCode.SUCCESS.toJSON();
	}
	
	public void sendMailing(String body,String receiver,String info){
		List<String> receiveList = new ArrayList<String>();
        for(String receive:receiver.split(",")){
	    	if(!receive.equals("")){
	    		receiveList.add(receive);
	    	}
	    }
        
        Smtp s = new Smtp();
        try {
			s.sendAttachment("卡盟", info, receiveList,body);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}
}
